import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { fetchValidation } from '@/utils';
import {
  queryBatchList,
  batchImport,
  printBatch,
  queryDetailInfo,
  openBatch,
  closeBatch,
} from './service';


export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    import: Effect;
    print: Effect;
    detail: Effect;
    open: Effect;
    close: Effect;
  };
  reducers: {
    save: Reducer;
    saveDetail: Reducer;
    changePrint: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'logisCenterBatchList',

  state: {
    data: {
      list: [],
      count: 0,
    },
    detailInfo: {},
    printVisible: false,
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryBatchList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: data.body,
      });
    },
    * import({ payload }, { call, put }) {
      const formData = new FormData();
      Object.keys(payload.params).forEach((key: string) => {
        formData.append(key, payload.params[key]);
      });
      const response = yield call(batchImport, formData);
      const data = fetchValidation(response);
      if (data) {
        message.success(data.msg);
        yield put({
          type: 'fetch',
          payload: payload.query,
        });
      }
    },
    * print({ payload }, { call, put }) {
      const response = yield call(printBatch, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success(data.msg);
      }
    },

    * detail({ payload }, { call, put }) {
      const { status, ...query } = payload;
      const response = yield call(queryDetailInfo, query);
      const data = fetchValidation(response);
      const params = {
        ...data.body,
        status,
      };
      yield data && put({
        type: 'saveDetail',
        payload: params,
      });
      yield data && put({
        type: 'changePrint',
        payload: true,
      });
    },

    * open({ payload }, { call, put }) {
      const response = yield call(openBatch, payload.params);
      const data = fetchValidation(response);
      if (data) {
        message.success(data.msg);
        yield put({
          type: 'fetch',
          payload: payload.query,
        });
      }
    },
    * close({ payload }, { call, put }) {
      const response = yield call(closeBatch, payload.params);
      const data = fetchValidation(response);
      if (data) {
        message.success(data.msg);
        yield put({
          type: 'fetch',
          payload: payload.query,
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },

    saveDetail(state, { payload }) {
      return {
        ...state,
        detailInfo: payload,
      };
    },

    changePrint(state, { payload }) {
      return {
        ...state,
        printVisible: payload,
      };
    },
  },
};

export default Model;
